﻿using System;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.EFDBFirst;
using YiYiTong.Rocket.Model;
using System.ComponentModel;
using YiYiTong.Rocket.Model.Manager;
using YiYiTong.Rocket.Business;
using System.Collections.Generic;

namespace YiYiTong.Rocket.ManageWeb.Controllers
{
    public class CategoryController : BaseController
    {
        [Description("分类列表")]
        [ViewPage]
        public ActionResult GetList(CategoryListQueryModel queryModel)
        {
            queryModel.Trim();
            ViewBag.paramModel = queryModel;

            bool isQuery = false;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            //条件筛选
            var queryable = dbcontext.ADZ_GoodsCategory.Where(t => t.is_del == 0);
            if (!string.IsNullOrWhiteSpace(queryModel.category_name_en))
            {
                isQuery = true;
                queryable = queryable.Where(t => t.category_name_en.Contains(queryModel.category_name_en));
            }
            if (!string.IsNullOrWhiteSpace(queryModel.category_name))
            {
                isQuery = true;
                queryable = queryable.Where(t => t.category_name.Contains(queryModel.category_name));
            }
            if (queryModel.is_show != -10)
            {
                isQuery = true;
                queryable = queryable.Where(t => t.is_show == queryModel.is_show);
            }

            int rowcounts = queryable.Count();
            ViewData.Add("AllCount", rowcounts);

            List<ADZ_GoodsCategory> list = queryable.ToList();
            if (!isQuery)
            {
                DataTable dt = new ModelHandler<ADZ_GoodsCategory>().FillDataTable(list);
                if (dt != null && dt.Rows.Count > 0)
                {
                    //克隆原有数据表结构
                    DataTable dtGoodsCategory = dt.Clone();
                    dtGoodsCategory = Utils.GetDropItems(dt, dtGoodsCategory, "category_name", "category_id", "0", "\u3000", "parent_id");
                    list = new ModelHandler<ADZ_GoodsCategory>().FillModel(dtGoodsCategory);
                }
            }
            return View(list);
        }

        [Description("新增/编辑分类")]
        [ViewPage]
        public ActionResult Add(long id = 0)
        {
            ViewBag.Title = "新增商品分类";
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ADZ_GoodsCategory category = new ADZ_GoodsCategory();
            if (id > 0)
            {
                ViewBag.Title = "编辑商品分类";
                category = dbcontext.ADZ_GoodsCategory.FirstOrDefault(t => t.category_id == id && t.is_del == 0);
            }
            return View(category);
        }

        [Description("新增/编辑分类")]
        [HttpPost]
        public ActionResult Add(ADZ_GoodsCategory category)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            byte is_root = 0;
            int level = 1;
            string category_code = string.Empty;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            //参数验证
            if (string.IsNullOrWhiteSpace(category.category_name))
            {
                resMsg.Message = "分类名称不能为空";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            if (category.parent_id == 0)
                is_root = 1;
            else
                level = 2;

            Tuple<bool, string> result = null;
            if (category.category_id > 0)
            {
                //编辑
                var entity = dbcontext.ADZ_GoodsCategory.FirstOrDefault(t => t.category_id == category.category_id && t.is_del == 0);
                if (entity.category_name != category.category_name)
                {
                    //判断分类名称重复
                    if (dbcontext.ADZ_GoodsCategory.FirstOrDefault(t => t.category_name == category.category_name) != null)
                    {
                        resMsg.Message = "分类名称已存在";
                        return Json(resMsg, JsonRequestBehavior.AllowGet);
                    }
                }

                entity.category_name = category.category_name;
                entity.category_name_en = category.category_name_en;
                entity.is_show = category.is_show;
                entity.parent_id = category.parent_id;
                entity.is_root = is_root;
                dbcontext.SaveChanges();
            }
            else
            {
                //新增
                category.is_del = 0;
                category.is_root = is_root;
                category.create_time = DateTime.Now;
                //判断分类名称重复
                if (dbcontext.ADZ_GoodsCategory.FirstOrDefault(t => t.category_name == category.category_name) != null)
                {
                    resMsg.Message = "分类名称已存在";
                    return Json(resMsg, JsonRequestBehavior.AllowGet);
                }

                dbcontext.ADZ_GoodsCategory.Add(category);
                dbcontext.SaveChanges();
            }

            //开始设置code
            result = GoodsBiz.Instance.BuildCategoryCode(category.category_id, level);
            if (!result.Item1)
            {
                resMsg.Message = "编辑分类失败，请重试";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            var model = dbcontext.ADZ_GoodsCategory.FirstOrDefault(t => t.category_id == category.category_id && t.is_del == 0);
            model.category_code = result.Item2;
            dbcontext.SaveChanges();
            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        [Description("删除分类")]
        [HttpPost]
        public ActionResult ToDel(int id = 0)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var entity = dbcontext.ADZ_GoodsCategory.FirstOrDefault(t => t.category_id == id);
            if (entity != null)
            {
                //删除分类，有子分类同时删除  leev 2019-07-14
                var updateList = dbcontext.ADZ_GoodsCategory.Where(t => t.category_code.StartsWith(entity.category_code)).ToList();
                foreach (var item in updateList)
                {
                    item.is_del = 1;
                    dbcontext.SaveChanges();
                }

                resMsg.IsSuccess = true;
                resMsg.Message = "操作成功";
            }
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }
    }
}